/* Copyright 2011 Elektrobit Wireless Communications Oy (EB)


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


 *All rights reserved 
 *Version:Elektrobit_Qt_1.0.0_20110305 */


#include "pageservice.h"
#include <appcommon.h>
#include <libs/xmlreader.h>
#include <libs/propertyhelper.h>
#include <mvcframework/pageview.h>
#include <servicemanager.h>
#include <QFile>
#include <QMetaType>

PageService::PageService(QObject *parent) : Service(parent) {
}

int PageService::start() {
    QString file = this->property("page_node_file").toString();
    return getPageFromXml(file);
}

Page *PageService::getPage(const QString &nodeKey) {
    return this->mPages.value(nodeKey, NULL);
}

QHash<QString, QString> PageService::getParentProperties(const QString &nodeKey) {
    QHash<QString, QString> defaultHash;
    return this->mParentProperties.value(nodeKey, defaultHash);
}

int PageService::getPageFromXml(const QString &file) {
      QDomDocument doc;
      if(XmlReader::getXmlDocument(file, doc)) {
          return -1;
      }
      QDomElement node = doc.documentElement().firstChildElement("node");
      while( !node.isNull() ) {
          createPage(node);
          fillParentProperties(node);
          node = node.nextSiblingElement();
      }
      return 0;
}

void PageService::createPage(QDomElement &node) {
    Page *page = createPageByType(node.firstChildElement("page").text());
    if(page) {
        PageView *view = createPageView(node);
        view->setPage(page);
        PropertyHelper::bindProperties(page, node.firstChildElement("page_properties"));
        page->setPageView(view);
        if(node.hasAttribute("keyword")) {
            APPINFO << node.attribute("keyword");
            mPages.insert(node.attribute("keyword"), page);
        }
    } 
}

PageView *PageService::createPageView(QDomElement &node) {
    PageView *view = createPageViewByType(node.firstChildElement("view").text());
    if(view) {
        view->hide();
        view->setParent(SERVICE(BackgroundService)->getBackgroundWidget());
        PropertyHelper::bindProperties(view, node.firstChildElement("view_properties"));
        view->setCssFile(node.firstChildElement("css").text());
        view->setQmlFile(node.firstChildElement("qml").text());
        view->init();
    }
    return view;
}

void PageService::fillParentProperties(QDomElement &node) {
    if(node.hasAttribute("keyword")) {
        QHash<QString, QString> properties;
        PropertyHelper::getProperties(node.firstChildElement("parent_properties"), properties);
        mParentProperties.insert(node.attribute("keyword"), properties);
    }
}

Page *PageService::createPageByType(const QString &type) {
    int id = QMetaType::type(type.toUtf8().data());
    APPINFO << QMetaType::typeName(id) << id;
    Page *p = NULL;
    if(id) {
        void *page = QMetaType::construct(id);
        p = static_cast<Page *>(page);
    } else {
        APPWARAING << "page " << type << " create error";
    }
    return p;
}

PageView *PageService::createPageViewByType(const QString &type) {
    int id = QMetaType::type(type.toUtf8().data());
    APPINFO << QMetaType::typeName(id) << id;
    PageView *v = NULL;
    if(id) {
        void *view = QMetaType::construct(id);
        v = static_cast<PageView *>(view);
    } else {
        APPWARAING << "pageview " << type << " create error";
    }
    return v;
}
